package com.lul15834.orderfood.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lul15834.orderfood.R;
import com.lul15834.orderfood.activity.user.adapter.UserAddressAdapter;
import com.lul15834.orderfood.bean.AddressBean;
import com.lul15834.orderfood.dao.AddressDao;
import com.lul15834.orderfood.until.Tools;

import java.util.List;

public class UserAddress extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user_address);
        Toolbar toolbar=findViewById(R.id.user_address_toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(UserAddress.this, ManageUser.class);
                intent.putExtra("sta","1");
                startActivity(intent);
            }
        });
        RecyclerView listView=findViewById(R.id.user_address_listView);
        listView.setLayoutManager(new LinearLayoutManager(this));
        String account= Tools.getOnAccount(this);
        List<AddressBean> list=AddressDao.getAllAddressByUserId(account);
        UserAddressAdapter userAddressAdapter=new UserAddressAdapter(list);
        if (list==null||list.size()==0){
            listView.setAdapter(null);
        }else {
            listView.setAdapter(userAddressAdapter);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.user_address_add_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int a=item.getItemId();
        if (a==R.id.user_address_add){
            Intent intent=new Intent(this, AddUserAddress.class);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }
}